<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		/*$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'name', 'required');
		
		if ($this->form_validation->run() == FALSE)
		{  echo 'dddddddddddddddddddddddddddd';
			$this->load->view('myform');
		}
		else
		{echo 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr';
			$this->load->view('formsuccess');
		}*/
	}
	

	function login(){
		$this->form_validation->set_error_delimiters('<label class="label label-important">', '</label>');
		$this->form_validation->set_ruless('email','Email', 'trim|required|callback_login_check',$this->input->post('email'));
		$this->form_validation->set_ruless('password','Password', 'trim|required','');		
		$vewArray = $this->form_validation->error_arrayui();
		if ($this->form_validation->run() == FALSE){
			$vewArray = $this->form_validation->error_arrayui();
		}else{
			$log_Data = array(
					'USER_ID'  => $this->memberDetails->id,
					'FIRSTNAME'     => $this->memberDetails->fname,
					'EMAIL'     => $this->memberDetails->email,										
					'LOGGED_IN' => TRUE,
					'IS_ADMIN' => FALSE,
					'SESS_ID' => time()
			);
			$this->session->set_userdata($log_Data);
			$this->input->set_cookie("FIRSTNAME", $this->memberDetails->fname, '86500', '', '/', 'site_', FALSE);													
			$this->session->set_flashdata('message', 'Successfully Logged In');
			 redirect('member/home');
		}
		//$this->page("login",$vewArray);
		$this->load->view("login",$vewArray);
				
		}
	function home(){	
		$city_name = '';
		//echo uniqueid();
		
		
		//echo get_select_education($id=5);
		

		if(!$this->session->userdata("USER_ID"))
			redirect("member/login");
		
		$this->model_member->getAmember($this->session->userdata("USER_ID"));
		$vewArray = array();
		$living_state = '';
		$living_country = $this->model_location->get_country_name($this->model_member->memberDetails->living_country);
		$living_state   = $this->model_location->get_state_name($this->model_member->memberDetails->living_state);
		//$city_name      = $this->model_location->get_city_name($this->model_member->memberDetails->city);
	
			# UNIQUE ID
				$vewArray["unique_id"] = $this->model_member->memberDetails->unique_id;
			# NAME
				$vewArray["full_name"] = $this->model_member->memberDetails->fname." ".$this->model_member->memberDetails->lname;
			# Location 
				$vewArray["city"]    = $city = $city_name;
				$state = $living_state ? $living_state.',' : '';
				$vewArray["state"]   = $living_state ? $living_state : '';
				$vewArray["country"] = $country = $living_country ? $living_country : '';
				$vewArray["complete_address"] = $city.' ' . $state . $country;
				
				$vewArray["citizenship"] = $this->model_member->memberDetails->citizenship;
				
			# PICTURE
				$vewArray["profile_picture"] = $this->model_member->getProfilePicture();

				
			# AGE
				$post_date = ($this->model_member->memberDetails->dob);
				$vewArray["age"] =   floor((time() - ($post_date)) / 31556926);
				
			# Height
				$vewArray["height"] = $this->model_member->memberDetails->height;
				
			# Education 
				$vewArray["education"] = $this->model_member->memberDetails->education ? get_education($this->model_member->memberDetails->education) : '';	
			# Ocupation
				$vewArray["occupation"]= $this->model_member->memberDetails->occupation;
			
			# BASSIC DETAILS
				$vewArray["createdby"]= $this->model_member->memberDetails->createdby;
				
				$vewArray["gender"]= $this->model_member->memberDetails->gender;
				
				$vewArray["weight"]= $this->model_member->memberDetails->weight;
				$vewArray["maritalstatusid"]= $this->model_member->memberDetails->maritalstatusid;
				$vewArray["mother_tongue"]= $this->model_member->memberDetails->mother_tongue;
				$vewArray["body_type"]= $this->model_member->memberDetails->body_type;
				$vewArray["complexion"]= $this->model_member->memberDetails->complexion;
				$vewArray["physical_status"]= $this->model_member->memberDetails->physical_status;

			# About me 
				$vewArray["aboutme"]= $this->model_member->memberDetails->aboutme;
			# RELIGN
				$vewArray["religion"]= $this->model_member->memberDetails->religion;
				$vewArray["denomination"]= $this->model_member->memberDetails->denomination;
				$vewArray["cast"]= $this->model_member->memberDetails->cast;
				
				//$vewArray["education"]= $this->model_member->memberDetails->education;
				$vewArray["occupation"]= $this->model_member->memberDetails->occupation;
			#Professional Information	
				$employed_in_Id = $this->model_member->memberDetails->employed_in;
					if($employed_in_Id == 1)
						$vewArray["employed_in"] =  'Government';
					else if($employed_in_Id == 2)
						$vewArray["employed_in"] =  'Defence';
					else if($employed_in_Id == 3)
						$vewArray["employed_in"] =  'Private';  
					else if($employed_in_Id == 4)			 
						$vewArray["employed_in"] =  'Business';    
					else if($employed_in_Id == 5)			
						$vewArray["employed_in"] =  'Self Employed';    
					else if($employed_in_Id == 6)
						$vewArray["employed_in"] =  'Not Working';  
				
				$vewArray["income"]= $this->model_member->memberDetails->income;
				
				$income_type_id = $this->model_member->memberDetails->income_type;
					if($income_type_id == 1)
						$vewArray["income_type"] =  'Monthly';
					if($income_type_id == 2)
						$vewArray["income_type"] =  'Annual';	

				
			
		
		# FAMILY DETAILS
			$this->model_member->getAmember_family($this->session->userdata("USER_ID"));
			
			   $vewArray["family_value"]     = $this->model_member->memberDetailsFamily->family_value;
			   $vewArray["family_type"]      = $this->model_member->memberDetailsFamily->family_type;
			   $vewArray["family_status"]    = $this->model_member->memberDetailsFamily->family_status;
		#Life style
			   $vewArray["eating_habits"]    = $this->model_member->memberDetails->eating_habits;
			   $vewArray["smoking_habits"]   = $this->model_member->memberDetails->smoking_habits;
			   $vewArray["drinking_habits"]  = $this->model_member->memberDetails->drinking_habits;	   
					
		$this->page("content",$vewArray);	
		//$this->page("personal_home",$vewArray);				
	
	}	
	
	
	
	function logout(){
		if($this->session->userdata("USER_ID")){			
			$this->session->sess_destroy();
		}
		redirect('/');
	}
	
	function editprofile(){		
	
		$view_arr[] = '';
		$vewArray   = '';
		$vewArray["profile"] = '';
		$view_arr["images"] = '';
		$this->model_member->getAmember($this->session->userdata("USER_ID"));
		
		$living_country = $this->model_member->memberDetails->living_country;
		

		$vewArray["profile_picture"] = $this->model_member->getProfilePicture();
		//if(isset($this->input->post('basicinformation')));
		if ( isset($_POST['basicinformation']))
		{	
			//$this->model_member->update_basicinformation();
			$this->model_member->update_basicinformation();
			redirect('/member/editprofile','refresh');
			//echo $this->input->post('basicinformation');
		}
		if ( isset($_POST['location'])){
		
			$this->model_member->update_location();
			redirect('/member/editprofile','refresh');
		
		}
		
		
	
		if(($this->uri->segment(3)=='basicinformation') || $this->uri->segment(3)==''){
			$view_arr["createdby"] = get_select_box('m_actcreatedby',$this->model_member->memberDetails->createdby);
			$view_arr["fname"] = $this->model_member->memberDetails->fname;
			$view_arr["lname"] = $this->model_member->memberDetails->lname;
			$view_arr["db_date"]  = get_select_day(gmdate("d", $this->model_member->memberDetails->dob));
			$view_arr["db_month"] = get_select_month(gmdate("m", $this->model_member->memberDetails->dob));
			$view_arr["db_year"]  = get_select_year(gmdate("Y", $this->model_member->memberDetails->dob));
			$view_arr["height"]   = get_select_height($this->model_member->memberDetails->height);
			$view_arr["weight"]	  = get_select_wight($this->model_member->memberDetails->weight);
			
			$view_arr["body_type"] = $this->model_member->memberDetails->body_type;
			$view_arr["complexion"] = $this->model_member->memberDetails->complexion;
			$view_arr["physical_status"] = $this->model_member->memberDetails->physical_status;
			$view_arr["eating_habits"] = $this->model_member->memberDetails->eating_habits;
			$view_arr["smoking_habits"] = $this->model_member->memberDetails->smoking_habits;
			$view_arr["drinking_habits"] = $this->model_member->memberDetails->drinking_habits;
			$view_arr["occupation"] = $this->model_member->memberDetails->occupation;
			
			$view_arr["languages_known"] = get_selectbox('','m_languages',$m=1);
			
			
			$view_arr["aboutme"]  = $this->model_member->memberDetails->aboutme;
			$vewArray["profile"] = $this->load->view('profile_basicinformation', $view_arr,TRUE);
			
		}	
		else
			if($this->uri->segment(3)=='education'){
			if(isset($_POST['educationdetails']))
					$this->model_member->update_educationdetails();
					
			$view_arr["education_details"] = $this->model_member->memberDetails->education_details;
			$view_arr["occupation_details"] = $this->model_member->memberDetails->occupation_details;
			
			
			
			
			$view_arr["employed_in"] = $this->model_member->memberDetails->employed_in;				   
			$view_arr["education"] = get_select_education($this->model_member->memberDetails->education);
			$view_arr["occupation"] = get_select_occupation($this->model_member->memberDetails->occupation);
			$view_arr["income"] = $this->model_member->memberDetails->income;
			$view_arr["income_type"] = $this->model_member->memberDetails->income_type;
			
			
			$vewArray["profile"] = $this->load->view('profile_education', $view_arr,TRUE);
			
			}
		else
			if($this->uri->segment(3)=='family'){
			if(isset($_POST['family_details']))
					$this->model_member->update_family_details();	
			
			$this->model_member->getAmember_family($this->session->userdata("USER_ID"));
			$view_arr["family_contact_number"]  = $this->model_member->memberDetailsFamily->family_contact_number;
			$view_arr["family_value"] 			= $this->model_member->memberDetailsFamily->family_value;
		    $view_arr["family_type"]			= $this->model_member->memberDetailsFamily->family_type;			
			$view_arr["family_status"]			= $this->model_member->memberDetailsFamily->family_status;
			$view_arr["family_orgin"] 			= $this->model_member->memberDetailsFamily->family_orgin;
			$view_arr["fathers_occupation"]		= $this->model_member->memberDetailsFamily->fathers_occupation;
			$view_arr["mothers_occupation"] 	= $this->model_member->memberDetailsFamily->mothers_occupation;
			$view_arr["fathers_name"] 			= $this->model_member->memberDetailsFamily->fathers_name;
			$view_arr["mothers_name"] 			= $this->model_member->memberDetailsFamily->mothers_name;
			$view_arr["fathers_nativeplace"]    = $this->model_member->memberDetailsFamily->fathers_nativeplace;
			$view_arr["mothers_nativeplace"]    = $this->model_member->memberDetailsFamily->mothers_nativeplace;
			$view_arr["no_brothers"]    		= $this->model_member->memberDetailsFamily->no_brothers;
			$view_arr["no_brothers_married"]    = $this->model_member->memberDetailsFamily->no_brothers_married;
			$view_arr["no_sisters"]    			= $this->model_member->memberDetailsFamily->no_sisters;
			$view_arr["no_sisters_married"]  	= $this->model_member->memberDetailsFamily->no_sisters_married;
			$view_arr["fathers_housename"]      = $this->model_member->memberDetailsFamily->fathers_housename;
			$view_arr["mothers_housename"]      = $this->model_member->memberDetailsFamily->mothers_housename;
			$view_arr["about_family"]           = $this->model_member->memberDetailsFamily->about_family;
			
				
			$vewArray["profile"] = $this->load->view('profile_family', $view_arr,TRUE);
				
			}
		else
			if($this->uri->segment(3)=='hobbies')
		$vewArray["profile"] = $this->load->view('profile_hobbies', $view_arr,TRUE);
		else
		    if($this->uri->segment(3)=='partnerpreference'){
				if(isset($_POST['partner_preference']))
						$this->model_member->update_partner_preference();
				$this->model_member->get_partner_preference($this->session->userdata("USER_ID"));	
				
				$view_arr['marital_status'] = unserialize($this->model_member->memberPreferences->marital_status);
				$view_arr['have_children']  =   $this->model_member->memberPreferences->have_children;
				$view_arr['age_from'] 		=   get_select_age($this->model_member->memberPreferences->age_from);
				$view_arr['age_to']         =   get_select_age($this->model_member->memberPreferences->age_to);
				$view_arr['height_from']    =   get_select_height($this->model_member->memberPreferences->height_from);
				$view_arr['height_to']      =   get_select_height($this->model_member->memberPreferences->height_to);
				$view_arr['physical_status']=   $this->model_member->memberPreferences->physical_status;
				$view_arr['education']      =   $this->model_member->memberPreferences->education;
				$view_arr['occupation']     =   $this->model_member->memberPreferences->occupation;
				$view_arr['citizen']        =   $this->model_member->memberPreferences->citizen;
				$view_arr['residing_state'] =   $this->model_member->memberPreferences->residing_state;
				$view_arr['food_habit']     =   $this->model_member->memberPreferences->food_habit;
				$view_arr['smoking_habit']  =   $this->model_member->memberPreferences->smoking_habit;
				$view_arr['drinking_habit'] =   $this->model_member->memberPreferences->drinking_habit;
				$view_arr['annual_income']  =   $this->model_member->memberPreferences->annual_income;
				$view_arr['about_partner']  =   $this->model_member->memberPreferences->about_partner;


			$view_arr["db_date"]  = get_select_day(gmdate("d", $this->model_member->memberDetails->dob));
		
			//$view_arr["height"]   = get_select_height($this->model_member->memberDetails->height);
			//$view_arr["weight"]	  = get_select_wight($this->model_member->memberDetails->weight);
				
				print_r($view_arr['marital_status']);
				//exit;
				
				$vewArray["profile"] = $this->load->view('profile_partnerpreference', $view_arr,TRUE);
		   }
		else
			if($this->uri->segment(3)=='location'){
				# LOCATION
				
				$view_arr["country_living"] = $this->model_location->get_location($location='country',$living_country);
				$citizenship    = $this->model_member->memberDetails->citizenship;
				$view_arr["citizenship"]    = $this->model_location->get_location($location='country',$citizenship);
				$living_state    = $this->model_member->memberDetails->living_state;
				$view_arr["state"]    = $this->model_location->get_state($living_country,$living_state);
				
				$vewArray["profile"] = $this->load->view('profile_location', $view_arr,TRUE);
		
			}
		else
			if($this->uri->segment(3)=='email')
		$vewArray["profile"] = $this->load->view('profile_email', $view_arr,TRUE);
		else
			if($this->uri->segment(3)=='photos'){
					$view_arr["images"] = '';
					$this->model_member->getAllImages();
					if($this->model_member->num_rows){
						$view_arr = '';
						$view_arr["images"] = '';
						foreach ($this->model_member->query->result() as $row){
							if($row->profile_picture == 1)
								$check_status = 'checked="checked"';
							else
								$check_status = '';
						$view_arr["name"] = $row->name;
						$view_arr["images"].='&nbsp;&nbsp;&nbsp;<img width="75" height="75" border="0" alt="" style="vertical-align:middle;" src="'.base_url().'/uploads/crop/'.$row->name.'"></img> <input type="radio" '.$check_status.' name="RadioGroup1" value="'.$row->id.'" id="RadioGroup1_0" onclick="check(this.value)"  />';
						//'.base_url().'/'.$vewArray["name"].'"
						}
				
				}		
				$vewArray["profile"] = $this->load->view('profile_photos', $view_arr,TRUE);
			}
		else
			if($this->uri->segment(3)=='contactnumber')
		$vewArray["profile"] = $this->load->view('profile_contactnumber', $view_arr,TRUE);
		else
			if($this->uri->segment(3)=='changepassword')
		$vewArray["profile"] = $this->load->view('profile_changepassword', $view_arr,TRUE);
		else
			if($this->uri->segment(3)=='deactivate')
		$vewArray["profile"] = $this->load->view('profile_deactive', $view_arr,TRUE);
		else
			if($this->uri->segment(3)=='deleteprofile')
		$vewArray["profile"] = $this->load->view('profile_delete', $view_arr,TRUE);
		else
			if($this->uri->segment(3)=='alert')
		$vewArray["profile"] = $this->load->view('profile_managealert', $view_arr,TRUE);
		
		
		$this->page("editprofile",$vewArray);		
	
	}	
	
	/////////////////////////////////
	 # METHOD 1
	/*function upload_file(){
		

		$this->load->view('upload');
		
		}
	function crop_file(){
		$targ_w = $targ_h = 150;
		$jpeg_quality = 90;

		$src = 'http://localhost/new/CI/CodeIgniter/upload/pool.jpg';
		$img_r = imagecreatefromjpeg($src);
		$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

		imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
		$targ_w,$targ_h,$_POST['w'],$_POST['h']);

		header('Content-type: image/jpeg');
		imagejpeg($dst_r,'simpletext.jpg',$jpeg_quality);
		}	*/
	/////////////////////////////////
    # ACTIONS
    function email_check($email){
    	if(!$this->model_member->emailExists($email)){
    		return TRUE;
    	}else{
    		$this->form_validation->set_message('email_check', "Email Exists.");
    		return FALSE;
    	}
    }
    
    function login_check($email){
    	$password = $this->input->post("password",TRUE);
    	if($this->model_member->user_login($email,$password)){
    		$this->memberDetails = $this->model_member->memberDetails;
    		if($this->memberDetails->deleted){
    			$this->form_validation->set_message('login_check', "Your Accounbt was deleted.");
    			return FALSE;
    		}else if(!$this->memberDetails->status){
    			$this->form_validation->set_message('login_check', "Your account is inactive.");
    			return FALSE;
    		}else{		    		
    			return TRUE;
    		}
    	}else{
    		$this->form_validation->set_message('login_check', "Email / Password not correct.");
    		return FALSE;
    	}    	
    }
	
	function do_upload()
	{
		# CROP UPLOADED IMAGE AND UPLOAD
		if($this->input->post('crop')=='Crop Image'){
			$src = $this->input->post('full_url');
			$targ_w = $targ_h = 150;
			$jpeg_quality = 90;
			$img_r = imagecreatefromjpeg($src);
			$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
			imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
			$targ_w,$targ_h,$_POST['w'],$_POST['h']);
			imagejpeg($dst_r,'uploads/crop/'.$this->input->post('image_name'),$jpeg_quality);
			//echo 'Successfully Updated';
			//echo '<img src="'.base_url().'/uploads/crop/'.$this->input->post('image_name').'">';
			//exit;
			redirect('/member/editprofile/photos','refresh');
	    }
			
		$config['upload_path'] = './uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '10000';
		$config['max_width']  = '6000';
		$config['max_height']  = '6000';
		$config['file_name'] =time();

		$this->load->library('upload', $config);
		
		# UPLOAD ORGINAL IMAGE 
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());

			$this->load->view('success', $error);
		}
		else
		{   # SUCCESS ORGINAL IMAGE UPLOAD 
			$data = array('upload_data' => $this->upload->data());
			//print_r($data);
			foreach ($data as $entry) {
			  
				  $image_details['full_path'] =  $entry['full_path'];
				  $image_details['orig_name'] =  $entry['orig_name'];
				  $this->model_member->upoladimages($entry['orig_name']);
			
			}

			
			$this->load->view('upload', $image_details);
		}
	}
	
    
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */